package cn.zz.veggie.persistence.repository;

import java.time.LocalTime;

import org.springframework.stereotype.Repository;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.util.RandomUtil;
import cn.zz.veggie.model.domain.Admin;
import lombok.RequiredArgsConstructor;

/**
 * @author zhongq
 * @since 2024/5/13
 */
@Repository
@RequiredArgsConstructor
public class AdminAuthRepository {

//    private final RedisTemplate<String, String> redisTemplate;

    private final int TOKEN_SEQ_MARK = 1 << 6 - 1;

    public String genAdminLoginToken(Admin admin) {
        // 时间 + 序号 + 随机书
        // 14 + 6 + 6 + 6
        final String dateFormat = DatePattern.PURE_DATETIME_FORMATTER.format(LocalTime.now());
        final String nextSeq = getNextSeq();
        final String id = String.format("%06d", admin.getId());
        final String random = String.format("%06d", RandomUtil.randomInt(0, 1_000_000));

        // save token by user


        //
        return dateFormat + nextSeq + id + random;
    }


    private String getNextSeq() {
        String prefix = "v:login:seq:" + DatePattern.PURE_DATE_FORMAT.format(LocalTime.now());
//        final Long increment = redisTemplate.opsForValue().increment(prefix) & TOKEN_SEQ_MARK;
        return String.format("%06d", 0);
    }


}